﻿using STF.Blog.Dtos;
using STF.Blog.IRepository;
using STF.Blog.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace STF.Blog.Service
{
    public class LevelService : ILevelService
    {
        private ILevelManager _le;
        public LevelService(ILevelManager le)
        {
            _le = le;
        }
        public async Task<int> AddLevelAsync(string title, string details)
        {
            return await _le.AddAsync(new Model.Level 
            {
                Title = title,
                Details = details
            });
        }

        public async Task<int> DeleteLevelAsync(int id)
        {
            var data = await _le.GetById(id);
            if(data == null)
                return -1;
            if(data.DeleteId != 1)
                return -2;
            return await _le.DeleteAsync(data);
        }

        public async Task<List<LeveLDto>> GetAllLevelAsync(int deleteId = 1)
        {
            var data = await _le.GetChooseAsync(le => le.DeleteId == deleteId);
            return data == null ? new List<LeveLDto>() : data.Select(le => new LeveLDto 
            {
                Id = le.Id,
                Title = le.Title,
                Details = le.Details,
                CreateTime = le.CreateTime,
                UpdateTime = le.UpdateTime
            }).ToList();
        }

        public async Task<LeveLDto> GetByIdAsync(int id)
        {
            var data = await _le.GetById(id);
            return data == null ? new LeveLDto() : new LeveLDto 
            {
                Id = data.Id,
                Title = data.Title,
                Details = data.Details,
                CreateTime = data.CreateTime,
                UpdateTime = data.UpdateTime
            };
        }

        public async Task<List<LeveLDto>> GetByNameAsync(string title, int deleteId = 1)
        {
            var data = await _le.GetChooseAsync(le => le.Title.Contains(title) && le.DeleteId == deleteId);
            return data == null ? new List<LeveLDto>() : data.Select(le => new LeveLDto 
            {
                Id = le.Id,
                Title = le.Title,
                Details = le.Details,
                CreateTime = le.CreateTime,
                UpdateTime = le.UpdateTime
            }).ToList();
        }

        public async Task<int> PutTrashAsync(int id)
        {
            var data = await _le.GetById(id);
            if (data == null || data.DeleteId == 0)
                return -1;
            data.DeleteId = 0;
            return await _le.UpdateAsync(data);
        }

        public async Task<int> RestoreAsync(int id)
        {
            var data = await _le.GetById(id);
            if (data == null || data.DeleteId == 1)
                return -1;
            data.DeleteId = 1;
            return await _le.UpdateAsync(data);
        }

        public async Task<int> UpdateLevelAsync(int id, string title, string details)
        {
            var data = await _le.GetById(id);
            if (data == null)
                return -1;
            data.Title = title;
            data.Details = details;
            data.UpdateTime = DateTime.Now;
            return await _le.UpdateAsync(data);
        }
    }
}
